<template>
	<el-dialog :title="dialogTitle" :visible.sync="visible" :close-on-click-modal="false" width="600px" custom-class="form_dialog_wrapper" @closed="handleDialogClosed">
		<el-form ref="form" :model="formData" :rules="formRules" label-width="100px" size="medium" class="form_item_class">
			<el-form-item :label="typeLabel + '名称'" prop="name" label-width="80px">
				<el-input type="input" v-model="formData.name" maxlength="64" show-word-limit></el-input>
			</el-form-item>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="上级菜单" label-width="80px">
						<sw-search-tree-select v-model="formData.pid" :default-props="defaultProps" :tree-data="treeListData" style="width: 100%"></sw-search-tree-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="角色类型">
						<el-select v-model="formData.style" placeholder="选择角色类型">
							<el-option label="全部" value="0"></el-option>
							<el-option v-for="(item, index) in dictEnums['ROLE_TYPE']" :key="index" :label="item.msg" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="是否业务菜单">
						<el-radio-group v-model="formData.isBusi">
							<el-radio :label="1">是</el-radio>
							<el-radio :label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="是否开发菜单">
						<el-radio-group v-model="formData.isDev">
							<el-radio :label="1">是</el-radio>
							<el-radio :label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="是否激活">
						<el-radio-group v-model="formData.del">
							<el-radio :label="1">是</el-radio>
							<el-radio :label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="是否公共菜单">
						<el-radio-group v-model="formData.isPublic">
							<el-radio :label="1">是</el-radio>
							<el-radio :label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item :label="typeLabel + '排序'" label-width="80px">
						<el-input-number v-model="formData.sort" controls-position="right" :min="1" style="width: 150px"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="level !== 4">
					<el-form-item label="菜单图标">
						<select-icon v-model="formData.icon"></select-icon>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="菜单路径" prop="url" label-width="80px" v-if="level !== 4">
				<el-input type="input" v-model="formData.url"></el-input>
			</el-form-item>
			<el-form-item :label="typeLabel + '描述'" prop="content" label-width="80px">
				<el-input type="textarea" v-model="formData.content"></el-input>
			</el-form-item>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" @click="handleSubmitForm">提交</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { mapGetters } from 'vuex';
import selectIcon from './selectIcon';
import { addedMenu, updateMenu } from '@/api/system/tbSysMenu';

export default {
	components: { selectIcon },
	props: {
		value: {
			type: Boolean,
			default: false
		},
		level: Number,
		pid: String,
		menuData: {
			type: Object,
			defalut() {
				return {};
			}
		},
		menuListData: {
			type: Array,
			default() {
				return [];
			}
		}
	},
	data() {
		return {
			visible: this.value,
			formData: {
				pid: '0',
				sort: 1,
				isBusi: 1,
				isDev: 2,
				del: 1,
				isPublic: 1
			},
			formRules: {
				name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
			},
			defaultProps: {
				children: 'childList',
				label: 'name'
			}
		};
	},
	computed: {
		...mapGetters(['dictEnums']),
		addOrEdit() {
			return Object.keys(this.menuData).length === 0;
		},
		typeLabel() {
			if (this.level === 4) {
				return '按钮';
			} else {
				return '菜单';
			}
		},
		dialogTitle() {
			let text = this.addOrEdit ? '添加' : '修改';
			if (this.level === 1) {
				return `${text}一级菜单`;
			} else if (this.level === 2) {
				return `${text}二级菜单`;
			} else if (this.level === 3) {
				return `${text}三级级菜单`;
			} else if (this.level === 4) {
				return `${text}按钮`;
			}
		},
		flatMenuData() {
			let datas = this.$lodash.cloneDeep(this.menuListData);
			let flatData = this.$treeToFlat(datas).filter((item) => item.menuLevel !== '4');
			if (this.level === 4) {
				return flatData;
			} else {
				return flatData.filter((item) => item.menuLevel !== '3');
			}
		},
		treeListData() {
			return [
				{
					id: '0',
					name: '顶级菜单',
					childList: this.$flatToTree(this.flatMenuData, 'pid')
				}
			];
		}
	},
	watch: {
		value(val) {
			this.visible = val;
		},
		visible(val) {
			this.$emit('input', val);
			if (!this.addOrEdit) {
				let obj = { ...this.menuData };
				obj['del'] = Number(obj.del);
				obj['isDev'] = Number(obj.isDev);
				obj['isBusi'] = Number(obj.isBusi);
				obj['isPublic'] = Number(obj.isPublic);
				this.formData = obj;
			}
			this.formData.pid = this.pid;
			if (this.level !== 4) {
				this.$set(this.formRules, 'url', { required: true, message: '请输入菜单路径', trigger: 'blur' });
			}
		}
	},
	methods: {
		//提交表单
		handleSubmitForm() {
			this.$refs['form'].validate((valid) => {
				if (!valid) return false;
				let params = this.formatParams();
				this.addOrEdit ? this.addFormRequest(params) : this.editFormRequest(params);
			});
		},

		//提交参数格式化
		formatParams() {
			let obj = { ...this.formData };
			if (this.level === 4) {
				obj['url'] = '';
				obj['icon'] = '';
				obj['menuLevel'] = 4;
			} else {
				let info = this.flatMenuData.find((item) => item.id === this.formData.pid);
				obj['menuLevel'] = !info ? 1 : Number(info.menuLevel) + 1;
			}
			return obj;
		},

		//添加表单请求
		async addFormRequest(params) {
			let result = await addedMenu(params).then((res) => res.data);
			if (result.code === '0') {
				this.visible = false;
				this.$emit('refresh');
				this.$showCustomMessage('success', `菜单添加成功`);
			}
		},

		//修改表单请求
		async editFormRequest(params) {
			let result = await updateMenu(params).then((res) => res.data);
			if (result.code === '0') {
				this.visible = false;
				this.$emit('refresh');
				this.$showCustomMessage('success', `菜单修改成功`);
			}
		},

		//重置表单
		handleDialogClosed() {
			this.formData = { pid: '0', sort: 1, isBusi: 1, isDev: 2, del: 1, isPublic: 1 };
		}
	}
};
</script>

<style lang="scss" lang="scss"></style>
